$(function () {
    var form = layui.form
    var layer = layui.layer
    var laypage = layui.laypage

    var q = {
        pagenum: 1,//页码值
        pagesize: 2,//每页显示多少条数据
        cate_id: '',//文章分类的 id
        state: '',//文章的发布状态
    }
    template.defaults.imports.dataFormat = function (date) {
        const dt = new Date(date)
        var y = dt.getFullYear()
        var m = padZero(dt.getMonth() + 1)
        var d = padZero(dt.getDate())
        var hh = padZero(dt.getHours())
        var mm = padZero(dt.getMinutes())
        var ss = padZero(dt.getSeconds())
        return y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss
    }

    function padZero(n) {
        return n > 9 ? n : '0' + n
    }


    inTable()
    inCate()

    function inTable() {
        $.ajax({
            type: 'get',
            url: '/my/article/list',
            data: q,
            success: function (res) {
                console.log(res);
                if (res.code !== 0) {
                    return layer.msg('获取文章列表失败！')
                }
                var htmlStr = template('tpl-table', res)
                $('tbody').html(htmlStr)

                renderPage(res.total)
            }

        })
    }

    function inCate() {
        $.ajax({
            type: 'get',
            url: '/my/cate/list',
            success: function (res) {
                console.log(res);
                if (res.code !== 0) {
                    return layer.msg('获取分类列表失败！')
                }

                var htmlStr = template('tpl-cate', res)
                $('[name=cate_id]').html(htmlStr)
                form.render()
            }

        })
    }

    $('#form-search').on('submit', function (e) {
        e.preventDefault()
        var cate_id = $('[name=cate_id]').val()
        var state = $('[name=state]').val()
        q.cate_id = cate_id
        q.state = state
        inTable()
    })

    function renderPage(total) {
        // console.log(totar);
        laypage.render({
            elem: 'test1',
            count: total,
            limit: q.pagesize,
            curr: q.pagenum,
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 3, 5, 10],
            jump: function (obj, first) {
                console.log(obj.curr);
                q.pagenum = obj.curr
                q.pagesize = obj.limit
                if (!first) {
                    inTable()
                }
            }

        })
    }


    $('body').on('click', '#btn-sc', function (e) {
        var len = $('#btn-sc').length
        console.log(len);
        var id = $(this).attr('data-id')
        layer.confirm('确认删除?', { icon: 3, title: '提示' }, function (index) {
            $.ajax({
                method: 'DELETE',
                url: '/my/article/info/?id=' + id,
                success: function (res) {
                    if (res.code !== 0) {
                        return layer.msg("删除文章分类失败！")
                    }

                    layer.msg("删除文章分类成功！")

                    if (len === 1) {
                        q.pagenum = q.pagenum === 1 ? 1 : q.pagenum - 1
                    }
                    inTable()
                }
            })

            layer.close(index);
        });

    })

    var indexEdit = null
    $('body').on('click', '.btn-edit', function (e) {
        // console.log('ok');
        indexEdit = layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: '修改文章分类',
            content: $('#dialog-edit').html(),
        });

        var id = $(this).attr('data-id')
        // var id = $(this).siblings('#id').val()
        // console.log(id);

        $.ajax({
            type: 'GET',
            url: '/my/article/info/?id=' + id,
            success: function (res) {
                console.log(res);
                form.val('form-edit', res.data)
            }
        })

    })


    $('body').on('submit', '#form-edit', function (e) {
        e.preventDefault()
        $.ajax({
            method: 'PUT',
            url: '/my/article/info',
            data: $(this).serialize(),
            success: function (res) {
                if (res.code !== 0) {
                    return layer.msg("修改列表分类失败！")
                }

                layer.msg("修改列表分类成功！")
                layer.close(indexEdit)
                inTable()
            }
        })
    })
})